package com.hemaapp.yjnh.model;

import android.content.Context;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.HemaNetWorker;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseNetTaskExecuteListener;
import com.hemaapp.yjnh.BaseNetWorker;
import com.hemaapp.yjnh.bean.Order;
import com.hemaapp.yjnh.bean.OrderDetail;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.present.BuyConfirmPresent;
import com.hemaapp.yjnh.present.OrderPresent;

import java.util.ArrayList;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/9/5 13:24
 * 修改备注：
 */
public class OrderTask extends BaseNetTaskExecuteListener {

    User user;
    BaseNetWorker netWorker;
    OrderPresent present;

    public OrderTask(Context context, User user, OrderPresent present) {
        super(context);
        this.user = user;
        this.present = present;
        netWorker = new BaseNetWorker(context);
        netWorker.setOnTaskExecuteListener(this);
    }

    public void getOrderDetail(String orderId) {
        netWorker.orderGet(user.getToken(), orderId);
    }

    public void operateOrder(String orderId, String keytype, String reason,
                             String description, String shipping_name, String shipping_num) {
        netWorker.orderSaveoperate(user.getToken(), orderId, keytype, reason,
                description, shipping_name, shipping_num);
    }

    @Override
    public void onPreExecute(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask) {
        present.beforeLoad();
    }

    @Override
    public void onPostExecute(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask) {
        present.loadFinish();
    }

    @Override
    public void onServerSuccess(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {
        BaseHttpInformation information = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (information) {
            case ORDER_SAVEOPERATE:
                present.onOrderOperateSuccess(hemaNetTask.getParams().get("keytype"));
                break;
            case ORDER_GET:
                HemaArrayResult<Order> bResult = (HemaArrayResult<Order>) hemaBaseResult;
                present.setmOrder(bResult.getObjects().get(0));
                break;
        }
    }

    @Override
    public void onServerFailed(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {
        BaseHttpInformation information = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (information) {
            case ORDER_SAVEOPERATE:
                present.onNetFailed("操作失败，"+hemaBaseResult.getMsg());
                break;
            case ORDER_GET:
                present.onNetFailed("获取订单信息失败，"+hemaBaseResult.getMsg());
                break;
        }
    }

    @Override
    public void onExecuteFailed(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, int i) {
        BaseHttpInformation information = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (information) {
            case ORDER_SAVEOPERATE:
                present.onNetFailed("操作失败，请稍后再试");
                break;
            case ORDER_GET:
                present.onNetFailed("获取订单信息失败，请稍后再试");
                break;
        }
    }
}
